package com.xiyi.shouxin.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {

    private static final char[] hextable = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
            'f' };

    public static String MD5Encrypt(String src) throws NoSuchAlgorithmException {
        // 创建MD5加密对象
        MessageDigest md = MessageDigest.getInstance("MD5");
        // 执行加密操作
        byte[] messageDigest = md.digest(src.getBytes());
        // 将字节数组转换为16进制字符串
        StringBuilder hexString = new StringBuilder();
        for (byte b : messageDigest) {
            hexString.append(hextable[(b& 0xff) >> 4]);
            hexString.append(hextable[(b& 0xff) & 0x0f]);
        }
        // 返回加密后的字符串
        return hexString.toString();
    }

    public static void main(String[] args) throws NoSuchAlgorithmException {
        System.out.println( MD5Encrypt("123456"));
    }
}
